package org.bot.server.location.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import org.bot.client.driver.api.server.ServerUpdateClientStatus;
import org.bot.client.driver.api.server.ServerReceiveClientData;
import org.bot.server.client.entity.BotClient;
import org.bot.server.client.mapper.BotClientMapper;
import org.bot.server.debug.data.DebuggerData;
import org.bot.server.event.entity.BotEvent;
import org.bot.server.event.mapper.BotEventMapper;
import org.bot.server.job.FlowQuartzJob;
import org.bot.server.location.mapper.ServerFlowInfoMapper;
import org.bot.server.location.mapper.ServerFlowLogInfoMapper;
import org.bot.server.location.mapper.ServerFlowLogMapper;
import org.bot.server.log.entity.BotClientLog;
import org.bot.server.log.entity.BotClientLogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 沧月
 * @create 0002 2023/10/2 16:29
 * @description <description class purpose>
 */

@Component
public class ServerFlowInfoImpl implements ServerReceiveClientData, ServerUpdateClientStatus {

    @Autowired
    ServerFlowInfoMapper serverFlowInfoMapper;

    @Autowired
    ServerFlowLogMapper serverFlowLogMapper;

    @Autowired
    ServerFlowLogInfoMapper serverFlowLogInfoMapper;

    @Autowired
    BotEventMapper botEventMapper;

    @Autowired
    DebuggerData debuggerData;

    @Autowired
    FlowQuartzJob quartzJob;

    @Autowired
    BotClientMapper botClientMapper;

    @Override
    public void clientOnline(String clientId) {
        serverFlowInfoMapper.updateClientStatus(clientId, 2);
        quartzJob.addClientJob(clientId);
    }

    @Override
    public void clientOffline(String clientId) {
        serverFlowInfoMapper.updateClientStatus(clientId, 1);
        quartzJob.deleteClientJob(clientId);
    }

    @Override
    public void clientLog(JSONObject log) {
        //存储客户端日志信息
        System.out.println("收到客户端日志::" + log.toString());
        String batchId = log.getString("batchId");
        QueryWrapper<BotClientLog> clientLogQueryWrapper = new QueryWrapper<>();
        clientLogQueryWrapper.eq("batch_id", batchId);
        BotClientLog _botClientLog = serverFlowLogMapper.selectOne(clientLogQueryWrapper);
        if (_botClientLog != null) {
            //更新状态操作
            _botClientLog.setStatus(log.getInteger("code"));
            _botClientLog.setEndTime(log.getString("endTime"));
            serverFlowLogMapper.updateById(_botClientLog);

            //更新日志明细
            BotClientLogInfo botClientLogInfo = new BotClientLogInfo();
            botClientLogInfo.setLogInfo(_botClientLog.getId());

            UpdateWrapper<BotClientLogInfo> botClientLogInfoUpdateWrapper = new UpdateWrapper<>();
            botClientLogInfoUpdateWrapper.eq("batch_id", batchId);
            serverFlowLogInfoMapper.update(botClientLogInfo, botClientLogInfoUpdateWrapper);
        } else {
            //新增操作
            BotClientLog botClientLog = new BotClientLog();
            botClientLog.setStatus(log.getInteger("code"));
            botClientLog.setFlowId(log.getInteger("flowId"));
            botClientLog.setCreationDate(new Date());
            botClientLog.setStartTime(log.getString("startTime"));
            botClientLog.setBatchId(batchId);
            serverFlowLogMapper.insert(botClientLog);
        }
    }

    @Override
    public void clientExecLog(JSONObject _tmp) {
        System.out.println("收到客户端执行日志::" + _tmp.toString());
        BotClientLogInfo botClientLogInfo = new BotClientLogInfo();
        botClientLogInfo.setArg(_tmp.getString("arg"));
        botClientLogInfo.setEvent(_tmp.getString("event"));
        botClientLogInfo.setNodeName(_tmp.getString("flowName"));
        botClientLogInfo.setStatus(_tmp.getString("status"));
        botClientLogInfo.setBatchId(_tmp.getString("batchId"));
        botClientLogInfo.setErrorMsg(_tmp.getString("errorMsg"));
        botClientLogInfo.setStartTime(_tmp.getString("startTime"));
        botClientLogInfo.setEndTime(_tmp.getString("endTime"));
        botClientLogInfo.setFlowOrder(_tmp.getInteger("order"));
        serverFlowLogInfoMapper.insert(botClientLogInfo);
    }

    @Override
    public void clientEvent(String clientId, JSONArray data) {
        UpdateWrapper<BotEvent> wrapper = new UpdateWrapper<>();
        wrapper.eq("clientId", clientId);
        botEventMapper.delete(wrapper);
        data.forEach(e -> {
            JSONObject object = (JSONObject) e;
            BotEvent botEvent = new BotEvent();
            botEvent.setEvent(object.getString("event"));
            botEvent.setEventName(object.getString("eventName"));
            botEvent.setClientId(clientId);
            botEventMapper.insert(botEvent);
        });
    }

    @Override
    public void clientStatus(String clientId, JSONObject data) {
        BotClient botClient = new BotClient();
        botClient.setTaskQueue(data.getIntValue("queue"));
        botClient.setTaskActive(data.getIntValue("active"));

        UpdateWrapper<BotClient> wrapper = new UpdateWrapper<>();
        wrapper.eq("client_id",clientId);
        botClientMapper.update(botClient,wrapper);
    }
}
